import { showModal, showToast, requestPayment } from "../../utils/asyncWx"
import { request } from "../../request/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        address: {},
        cart: [],
        totalPrice: 0,
        totalNum: 0
    },


    // 点击支付 
    async handleOrderPay() {
        //  判断缓存中有无 token
        const token = wx.getStorageSync("token");
        // 判断
        if (token === "") {
            wx.navigateTo({
                url: '/pages/auth/index'
            });
            return;
        }
        //创建订单
        const header = { Authorization: token };
        const order_price = this.data.totalPrice;
        const consignee_addr = this.data.address.all;
        const cart = this.data.cart;
        let goods = [];
        cart.forEach(v => goods.push({
            goods_id: v.goods_id,
            goods_id: v.goods_id,
            goods_price: v.goods_price
        }))
        const orderParams = { order_price, consignee_addr, goods }
            // 发送请求 获取订单
            // const res = await request({ url: "/my/orders/create", method: "POST", data: orderParams, header });
        const { pay } = {
            nonceStr: "U6tYjNdYvm3ReKgI",
            package: "prepay_id=wx09182118356902a15c8b8d071931343000",
            paySign: "C514E29387794F84884C983AFFF4787F",
            signType: "MD5",
            timeStamp: "1565346079"
        }

        const res = requestPayment(pay);
        //删除选中的商品
        let newCart = wx.getStorageSync("cart");
        newCart = newCart.filter(v => !v.checked);
        wx.setStorageSync("cart", newCart);
        wx.navigateTo({
            url: '/pages/order/index',

        });


    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        //得到缓存中的地址
        let address = wx.getStorageSync("address");
        let cart = wx.getStorageSync("cart") || [];
        if (address != '') {
            address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
        }
        //过滤数组
        cart = cart.filter(v => v.checked);

        //总价格 总数量
        let totalPrice = 0;
        let totalNum = 0;
        cart.forEach(v => {
            totalPrice += v.num * v.goods_price;
            totalNum += v.num
        });

        //重置
        this.setData({
            cart,
            totalPrice,
            totalNum,
            address
        })
    },


})